package com.jming.jike.common;

import org.springframework.util.StringUtils;

import java.io.Serializable;

import static com.jming.jike.common.ResponseCode.SUCCESS;


/**
 * 响应结果
 *
 * @author jming
 */
public class ResponseResult<T> implements Serializable {

    private String code;
    private String message;
    private T data;

    public ResponseResult() {
    }

    public ResponseResult(T data) {
        this.code = SUCCESS.getCode();
        this.message = SUCCESS.getMessage();
        this.data = data;
    }

    public ResponseResult(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseResult(String code, T data) {
        this.code = code;
        this.message = SUCCESS.getMessage();
        this.data = data;
    }

    public ResponseResult(String code, String message, T data) {
        this.code = code;
        this.message = SUCCESS.getMessage();
        this.data = data;
    }

    public boolean isSuccess() {

        return StringUtils.hasText(code) && (SUCCESS.getCode().equals(code));
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> ResponseResult<T> success(String code, String message, T data) {
        return new ResponseResult<>(code, message, data);
    }

    public static <T> ResponseResult<T> success(String message, T data) {
        return new ResponseResult<>(SUCCESS.getCode(), message, data);
    }

    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(SUCCESS.getCode(), SUCCESS.getMessage(), data);
    }

    public static <T> ResponseResult<T> exception(String code, String message) {
        return new ResponseResult<>(code, message, null);
    }

    public static <T> ResponseResult<T> exception(String message) {
        return new ResponseResult<>(ResponseCode.BUSINESS_EXCEPTION.getCode(), message, null);
    }

    public static <T> ResponseResult<T> exception(String code, String message, T data) {
        return new ResponseResult<>(code, message, data);
    }

    public static <T> ResponseResult<T> fail(String code, String message) {
        return new ResponseResult<>(code, message, null);
    }

    public static <T> ResponseResult<T> fail(String message) {
        return new ResponseResult<>(ResponseCode.BUSINESS_EXCEPTION.getCode(), message, null);
    }
}